--
-- Author: yawei.zhu
-- Date: 2018-05-31 17:54:28
--

local DungeonInfo = class("DungeonInfo")

function DungeonInfo:ctor(node)
	self.m_bossNode = node

	self:initDungeonNodes()
end


function DungeonInfo:initDungeonNodes()
    local rootPanel = self.m_bossNode:getChildByName("Panel")
    self.dungeon_Text_name = rootPanel:getChildByName("Text_name")

    self._bossAmiPanel = rootPanel:getChildByName("Panel_ami")
    self._bossArrowNode = rootPanel:getChildByName("arrow")

    self._dungeon_TouchPanel = rootPanel:getChildByName("bg01")

    local normalRootPanel = self._bossAmiPanel:getChildByName("Panel")
    self._dungeon_Text_time = normalRootPanel:getChildByName("Text_time")
    self._dungeon_Text_kill = normalRootPanel:getChildByName("Text_kill")
    self._dungeon_Text_over = normalRootPanel:getChildByName("Text_over")
    self._dungeon_Text_Count = normalRootPanel:getChildByName("Text_count")
    self._dungeon_Icon_Count = normalRootPanel:getChildByName("icon_count")
    if self._dungeon_Text_Count then
        self._dungeon_Text_Count:setVisible(false)
    end
    if self._dungeon_Icon_Count then
        self._dungeon_Icon_Count:setVisible(false)
    end

    self._dungeon_Panel_time = self._bossAmiPanel:getChildByName("Panel_time")
    self._dungeon_Text_count = self._dungeon_Panel_time:getChildByName("Text_count")
    self._dungeon_medal_1 = self._dungeon_Panel_time:getChildByName("medal_1")
    self._dungeon_medal_2 = self._dungeon_Panel_time:getChildByName("medal_2")
    self._dungeon_medal_3 = self._dungeon_Panel_time:getChildByName("medal_3")
    self._dungeon_line_1 = self._dungeon_Panel_time:getChildByName("line_1")
    self._dungeon_line_2 = self._dungeon_Panel_time:getChildByName("line_2")
    self._dungeon_line_3 = self._dungeon_Panel_time:getChildByName("line_3")

    self._dungeon_line_1:setVisible(false)
    self._dungeon_line_2:setVisible(false)
    self._dungeon_line_3:setVisible(false)
end

function DungeonInfo:showDungeonUi()
    if not self._bossAction then
        self._bossAction = cc.CSLoader:createTimeline(Res.DesktopDungeonBossNode)
        self.m_bossNode:runAction(self._bossAction)
        self._bossAction:gotoFrameAndPause(0)
    end
    if not self.m_bossNode:isVisible() then
        self.m_bossNode:setVisible(true)

        self.dungeon_Text_name:setString(DungeonManager:getDungeonName())

        -- local action = cc.RepeatForever:create(
        --     cc.Sequence:create(
        --     cc.DelayTime:create(1),
        --     cc.CallFunc:create(function()
        --     self:updateDungeonUi()
        -- end)))

        -- self.m_bossNode:runAction(action)

        self._cronsIdx = scheduler.newCron(handler(self, self.updateDungeonUi), 1, true)

        self:updateDungeonUi()

        self:setDungeonUiFinished(false)

        self._dungeon_TouchPanel:setTouchEnabled(true)
        self._dungeon_TouchPanel:addClickEventListener(function(sender)
            if self._openBossStatus then
                self:closeBossAction()
            else
                self:openBossAction()
            end
        end)

        self:openBossAction()
    end

    self:initDungenUiContent()
end

function DungeonInfo:hideDungeonUi()
    self._bossAction = nil
    self._openBossStatus = false

    if self._cronsIdx then
        scheduler.cancelCron(self._cronsIdx)
        self._cronsIdx = nil
    end
end

function DungeonInfo:initDungenUiContent()
    local time1,time2,time3,isRace = DungeonManager:getDungeonRaceTime()
    if isRace then

        self._dungeon_medal_1:setString(string.format(L("lua_code_text_207"), time1))
        self._dungeon_medal_2:setString(string.format(L("lua_code_text_170"), time2))
        self._dungeon_medal_3:setString(string.format(L("lua_code_text_118"), time3))

        self._second1 = TimeUtil.timeToSeconds(time1)
        self._second2 = TimeUtil.timeToSeconds(time2)
        self._second3 = TimeUtil.timeToSeconds(time3)

        self._dungeon_line_1:setVisible(false)
        self._dungeon_line_2:setVisible(false)
        self._dungeon_line_3:setVisible(false)
    end
end

function DungeonInfo:updateDungeonUi()
    local killStr = DungeonManager:getDungeonLeaderProgress() or ""
    self._dungeon_Text_kill:setString(string.format(L("lua_code_text_404"), killStr)) 
    local timeStr = DungeonManager:getDungeonResidueTime() or ""
    self._dungeon_Text_time:setString(timeStr)
    self._dungeon_Text_time:setString(string.format(L("lua_code_text_531"), timeStr)) 

    local isRace,time,percent,isStop = DungeonManager:getDungeonTime()
    if isRace then
        self._dungeon_Panel_time:setVisible(true)
        if not isStop then
            self._dungeon_Text_count:setString(string.format(L("lua_code_text_139"), time))

            -- 划线
            local second = TimeUtil.timeToSeconds(time)
            if self._second1 and self._second1 < second then
                self._dungeon_line_1:setVisible(true)
            end
            if self._second2 and self._second2 < second then
                self._dungeon_line_2:setVisible(true)
            end
            if self._second3 and self._second3 < second then
                self._dungeon_line_3:setVisible(true)
            end
        end

    else
        self._dungeon_Panel_time:setVisible(false)
    end
end

function DungeonInfo:setDungeonUiFinished(isFinished)
    self._dungeon_Text_time:setVisible(true)
    self._dungeon_Text_kill:setVisible(true)
    self._dungeon_Text_over:setVisible(isFinished)
end

function DungeonInfo:openBossAction()
    if self._bossAction then
        if not self._openBossStatus then
            self._openBossStatus = true
            self:switchBossArrowStatus()
            self._bossAction:play("in", false)
            self._bossAction:clearLastFrameCallFunc()
            self._bossAction:setLastFrameCallFunc(function()

                self._bossAction:clearLastFrameCallFunc()
            end)
            self._bossAmiPanel:setVisible(true)
        end
    end
end

function DungeonInfo:closeBossAction()
    if self._bossAction then
        if self._openBossStatus then
            self._openBossStatus = false
            self:switchBossArrowStatus()
            self._bossAction:play("out", false)
            self._bossAction:clearLastFrameCallFunc()
            self._bossAction:setLastFrameCallFunc(function()
                self._bossAmiPanel:setVisible(false)
                self._bossAction:clearLastFrameCallFunc()
            end)
        end
    end
end

function DungeonInfo:switchBossArrowStatus()
    if self._openBossStatus then
        self._bossArrowNode:setRotation(0)
    else
        self._bossArrowNode:setRotation(90)
    end
end


return DungeonInfo
